import { createApp } from 'vue'

import App from './App.vue'
import "@/assets/less/index.less"
import router from './router'
import pinia from './stores'
// 全局导入ElementPlus
import ElementPlus from 'element-plus';
import "element-plus/dist/index.css"
// 导入ElementPlus图标
import * as ElementPlusIconsVue from '@element-plus/icons'
import locale from 'element-plus/es/locale/lang/zh-cn'; // 引入中文语言包  

// 引入mock
import "@/api/mock.js"
// 注册全局$api
import api from '@/api/api'
import { useAllDataStore } from "@/stores"

import * as fabric from 'fabric'


function isRoute(to){
    let res = router.getRoutes()
    let resFil =  res.filter((item) => item.path === to.path)
    return resFil.length > 0
}
router.beforeEach((to,from)=>{
    if(to.path !== '/login' && !store.state.token){
        return { name: 'login' }
    }  
    if(!isRoute(to)){
        return {name: '404'}
    }
})
const app = createApp(App)
app.config.globalProperties.$api = api
// 设置 Element Plus 的语言为中文  
app.use(ElementPlus,{ locale })
app.use(pinia)
app.use(fabric)
const store = useAllDataStore()
store.addMenu(router,"refresh")
app.use(router).mount('#app')
// 这里引入icon
for(const [key,component] of Object.entries(ElementPlusIconsVue)){
    app.component(key,component)
}
